package net.wanho.ems.controller;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import net.wanho.ems.vo.AjaxResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Author：汤小洋
 * Date：2023-07-07 10:01
 * Description：<描述>
 */
@RestController
public class FileController {

    @RequestMapping("/upload")
    public AjaxResult upload(@RequestParam CommonsMultipartFile file, HttpServletRequest req) throws IOException {
        String filename = IdUtil.simpleUUID() + "." + FileNameUtil.getSuffix(file.getOriginalFilename());
        String filePath = req.getServletContext().getRealPath("/upload/imgs") + File.separator + filename;

        file.transferTo(new File(filePath)); // 写入到磁盘

        return AjaxResult.success("/upload/imgs" + File.separator + filename);
    }

}
